import { Body, Controller, Delete, HttpCode, HttpStatus, Param, ParseIntPipe, Patch, Post, UseGuards, UseInterceptors, UploadedFile } from '@nestjs/common';
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CurrentUser, ICurrentUserType } from 'src/decorators/current_user';
import { PostService } from 'src/service/post/post.service';
import { PostCreateDto } from './dto/post.create.dto';
import { PostReqDto } from './dto/post.req.dto';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import { OssService } from 'src/service/upload/oss/oss.service';

@ApiTags('后台管理系统-文章管理')
@Controller('post')
export class PostController {
    constructor(
        private postService: PostService,
        private ossService: OssService,
    ) { }
    @ApiOperation({
        summary: '文章新增',
        description: '文章新增'
    })
    @ApiCreatedResponse({
        status: 200,
        description: '成功时返回的数据'
    })
    @Post('add')
    @UseGuards(AuthGuard('jwt'))
    @HttpCode(HttpStatus.OK)
    async postAdd(@CurrentUser() userInfo: ICurrentUserType, @Body() postCreateDto: PostCreateDto) {
        const message = await this.postService.postAdd(userInfo, postCreateDto)
        return message
    }

    @ApiOperation({
        summary: '文章删除',
        description: '文章删除'
    })
    @ApiCreatedResponse({
        status: 200,
        description: '成功时返回的数据'
    })
    @Delete('/:id')
    @UseGuards(AuthGuard('jwt'))
    @HttpCode(HttpStatus.OK)
    async postDelete(@Param('id', new ParseIntPipe()) id: number) {
        return await this.postService.destoryPostById(id)
    }

    @ApiOperation({
        summary: '文章修改',
        description: '文章修改'
    })
    @ApiCreatedResponse({
        status: 200,
        description: '成功时返回的数据'
    })
    @Patch('/:id')
    @HttpCode(HttpStatus.OK)
    async postUpdate(@Param('id', new ParseIntPipe()) id: number, @Body() body: PostCreateDto) {
        return await this.postService.modifyPostById(id, body)
    }

    // 查找所有
    @ApiOperation({
        summary: '文章查找分页',
        description: '文章查找分页'
    })
    @ApiCreatedResponse({
        status: 200,
        description: '成功时返回的数据'
    })
    @Post('find')
    @HttpCode(HttpStatus.OK)
    async postFind(@Body() body: PostReqDto) {
        return await this.postService.postListPage(body)
    }

    // 封面上传
    @ApiOperation({
        summary: '文章封面上传',
        description: '文章封面上传'
    })
    @ApiCreatedResponse({
        status: 200,
        description: '成功时返回的数据'
    })
    @Post('upload')
    @UseInterceptors(FileInterceptor('file'))
    @HttpCode(HttpStatus.OK)
    async upload(@UploadedFile() file) {
        const res = await this.ossService.putOssFile(file)
        return {
            res
        }
    }

}
